package com.walterweimod.auxiliary_attributes.event;

import com.walterweimod.auxiliary_attributes.attribute.ModAttributes;
import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeLivingEntityAttributeRangedValueBase;
import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingHealEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Objects;

import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.getLogger;
import static com.walterweimod.auxiliary_attributes.mobeffect.Rage2.*;
import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.addAttributeModifier;
import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.removeAttributeModifier;
import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.getCapabilityValue;
import static com.walterweimod.auxiliary_attributes.util.ModUtils.getLivingEntityHealthPercent;
import static com.walterweimod.auxiliary_attributes.util.ModUtils.hyphenName;

@Mod.EventBusSubscriber
public class Rage2Event {
    @SubscribeEvent
    public static void afterHealUpdateMinRage(LivingHealEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance rage2 = livingEntity.getEffect(ModMobEffects.RAGE2.get());

        if (Objects.nonNull(rage2)){
            modifyAttributes(livingEntity, rage2.getAmplifier());
        }
    }

    @SubscribeEvent
    public static void afterDamageUpdateMinRage(LivingDamageEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance rage2 = livingEntity.getEffect(ModMobEffects.RAGE2.get());

        if (Objects.nonNull(rage2)){
            modifyAttributes(livingEntity, rage2.getAmplifier());
        }
    }

    static void modifyAttributes(LivingEntity livingEntity, int amplifier){
        if (Objects.nonNull(livingEntity)){
            AttributeInstance min_rage = livingEntity.getAttribute(ModAttributes.MIN_RAGE.get());
            AttributeInstance max_rage = livingEntity.getAttribute(ModAttributes.MAX_RAGE.get());
            float reducePercent = Math.min(50.0F + 2.0F * amplifier, 85.0F)
                    - getLivingEntityHealthPercent(livingEntity);
            boolean canIncrease = getLivingEntityHealthPercent(livingEntity)
                    <= Math.min(50.0F + 2.0F * amplifier, 85.0F);
            double increaseValue = canIncrease ? Math.ceil(reducePercent / 4) : 0.0D;

            getLogger().info(hyphenName("Rage2", 15));

            if (Objects.nonNull(min_rage)){
                AttributeModifier min_rage_modifier = min_rage.getModifier(getRage2MinRageUUID());
                double amount = Objects.nonNull(min_rage_modifier) ? min_rage_modifier.getAmount() : 0.0D;

                removeAttributeModifier(
                        livingEntity,
                        min_rage.getAttribute(),
                        getRage2MinRageUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        min_rage.getAttribute(),
                        15.0D + 5.0 * amplifier + increaseValue,
                        getRage2MinRageUUID(),
                        AttributeModifier.Operation.ADDITION,
                        false
                );

                if (Objects.nonNull(min_rage_modifier)){
                    double add_amount = 15.0D + 5.0 * amplifier + increaseValue - amount;

                    ModCapabilityUtils.setCapabilityValue(
                            livingEntity,
                            ModCapabilities.RAGE_LAYER_VALUE,
                            rageLayerValue -> rageLayerValue.addValue((int) add_amount)
                    );

                    getLogger().info("战意值：{}", getCapabilityValue(
                            livingEntity,
                            ModCapabilities.RAGE_LAYER_VALUE,
                            ChargeLivingEntityAttributeRangedValueBase::getValue,
                            0
                    ));
                }

                getLogger().info("最小战意值：{}", min_rage.getValue());
            }

            if (Objects.nonNull(max_rage)){
                removeAttributeModifier(
                        livingEntity,
                        max_rage.getAttribute(),
                        getRage2MaxRageUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        max_rage.getAttribute(),
                        10.0 * amplifier + increaseValue,
                        getRage2MinRageUUID(),
                        AttributeModifier.Operation.ADDITION,
                        false
                );

                getLogger().info("最大战意值：{}", max_rage.getValue());
            }
        }
    }
}
